package com.gqz.equip.app.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.equip.app.entity.EnStockPeriodTaskEntity;
import com.gqz.equip.app.mapper.EnStockPeriodTaskMapper;
import com.gqz.equip.app.mapstruct.EnStockPeriodTaskMapstruct;
import com.gqz.equip.app.service.IEnStockPeriodTaskService;
import com.gqz.equip.vo.EnStockPeriodTaskVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 能源-盘点工单-周期任务表 ҵ���߼�ʵ�ֲ�ServiceImpl
 *
 * @Date 2022-12-06 21:08:27
 * @Version 1.0
 * @Author input you name
 */
@Service
@Slf4j
public class EnStockPeriodTaskServiceImpl extends ServiceImpl<EnStockPeriodTaskMapper, EnStockPeriodTaskEntity> implements IEnStockPeriodTaskService {

    @Autowired
    private EnStockPeriodTaskMapstruct mapstruct;

    @Override
    public List<EnStockPeriodTaskVo> getById(String id) {
        MPJLambdaWrapper<EnStockPeriodTaskEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnStockPeriodTaskEntity::getId, id);
        queryWrapper.selectAll(EnStockPeriodTaskEntity.class);
        List<EnStockPeriodTaskEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnStockPeriodTaskVo> page(RequestForm<EnStockPeriodTaskVo> requestForm) {
        EnStockPeriodTaskEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnStockPeriodTaskEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(EnStockPeriodTaskEntity.class);
        Page<EnStockPeriodTaskEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnStockPeriodTaskEntity> iPage = baseMapper.selectJoinPage(page, EnStockPeriodTaskEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnStockPeriodTaskVo> requestForm) {
        EnStockPeriodTaskEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnStockPeriodTaskEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.select(EnStockPeriodTaskEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnStockPeriodTaskVo> forms) {
        List<EnStockPeriodTaskEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnStockPeriodTaskEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnStockPeriodTaskVo> forms) {
        List<EnStockPeriodTaskEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnStockPeriodTaskEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }

    /**
     * 获取需要生成任务的list
     *
     * @return 能源-盘点工单-周期任务
     */
    @Override
    public List<EnStockPeriodTaskVo> getStockPeriodTaskList(int offsetDay) {
        QueryWrapper<EnStockPeriodTaskEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EnStockPeriodTaskEntity::getExecuteStatus, "0");
        List<EnStockPeriodTaskEntity> enStockSheetEntities = baseMapper.selectList(queryWrapper);
        List<EnStockPeriodTaskVo> enStockPeriodTaskVos = new ArrayList<>();
        enStockSheetEntities.forEach(periodTaskEntity -> {
            String executeStime = DateUtil.formatDate(periodTaskEntity.getExecuteStime());
            if (DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(DateUtil.today()), offsetDay)).equals(executeStime)
                    && periodTaskEntity.getExecuteStime().before(periodTaskEntity.getPeriodEtime())) {
                enStockPeriodTaskVos.add(mapstruct.toVo(periodTaskEntity));
            }
        });
        return enStockPeriodTaskVos;
    }


}
